// file: cs-acs-graph.do
// purpose: create Fig 7. 

clear
use entropy.dta, clear

gen stateabv = upper(stusab)
foreach x in race faminc age ed fb {
graph dot (mean) H_`x'_cd H_`x'_u H_`x'_l H_`x'_ct  if level==1 , over(stateabv, sort(H_`x'_ct) label(labsize(*.5))) ///
    graphregion(color(white)) exclude0  ///
    marker(1, mcolor(white) msize(small) mlwidth(vthin) mlcolor(black)) ///
	marker(2, mcolor(gs12) msize(small)) ///
	marker(3, mcolor(gs7) msize(small)) ///
	marker(4, mcolor(gs0) msize(small)) linetype(line) lines(lwidth(vvthin)) ///
	ytitle("Theil's H") ///
	legend( label(1 "CD") label(2 "Upper") label(3 "Lower") label(4 "Census Tract")) ///
    legend(col(4) region(color(white%10) lstyle(none))) 

graph export H_`x'.pdf, replace
}


use entropy.dta, clear
gen stateabv = upper(stusab)
keep if level==1
keep stateabv e_*_state H_* 
foreach level in ct puma l u {	
foreach var in H_age_`level' H_race_`level' H_ed_`level' ///
	H_faminc_`level' H_fb_`level' {
quietly summarize `var', detail
gen std`var' = (`var' - r(mean)) / r(sd)
}	
}
save entropy_st.dta, replace

use entropy.dta, clear
gen stateabv = upper(stusab)
keep if level==4
keep geoid entropy_* 
foreach var of varlist * {
rename `var' `var'_u
}

foreach var in entropy_age entropy_race entropy_ed ///
	entropy_faminc entropy_fb {
quietly summarize `var', detail
replace `var' = (`var' - r(mean)) / r(sd)
}
save entropy_u.dta, replace

use entropy.dta, clear
gen stateabv = upper(stusab)
keep if level==5
keep geoid entropy_* 
foreach var of varlist * {
rename `var' `var'_l
}
foreach var in entropy_age entropy_race entropy_ed ///
	entropy_faminc entropy_fb {
quietly summarize `var', detail
gen std`var' = (`var' - r(mean)) / r(sd)
}
save entropy_l.dta, replace

use entropy.dta, clear
gen stateabv = upper(stusab)
keep if level==7
keep geoid entropy_* 
foreach var of varlist * {
rename `var' `var'_puma
}
foreach var in entropy_age entropy_race entropy_ed ///
	entropy_faminc entropy_fb {
quietly summarize `var', detail
gen std`var' = (`var' - r(mean)) / r(sd)
}
save entropy_puma.dta, replace

use entropy.dta, clear
gen stateabv = upper(stusab)
keep if level==2
keep geoid entropy_* 
foreach var of varlist * {
rename `var' `var'_cnty
}
foreach var in entropy_age entropy_race entropy_ed ///
	entropy_faminc entropy_fb {
quietly summarize `var', detail
gen std`var' = (`var' - r(mean)) / r(sd)
}
save entropy_cnty.dta, replace
